﻿var added = ""; // Para expandir el arbol en esa cuenta
var tree = tree || undefined;
    
function nodeLoad(node) {
    Ext.net.DirectMethods.NodeLoad(node.id, {
        success: function (result) {
            var data = eval("(" + result + ")");
            node.removeAll(1);
            node.loadNodes(data);
                       
            if(typeof added === "number")
                added = String(added);
            
            if(added != ""){
                                
                var path = "/0";
                
                for(var i = 0; i < Math.min(4, added.length); i++)
                {
                    path += "/";
                    path += added.substr(0, i+1);
                }
                
                added = "";
                
                setTimeout("tree.expandPath('"+path+"');", 2000);
            
            }
        },

        failure: function (errorMsg) {
            Ext.Msg.alert('Ha ocurrido un error', errorMsg);
        }
    });
}

var filterTree = function (el, e) {

    var text = this.getRawValue();

    tree.clearFilter();

    if (Ext.isEmpty(text, false)) {
        clearFilter();
        return;
    }
    

    if (e.getKey() === Ext.EventObject.ESC) {
        clearFilter();
    } else {
        var re = new RegExp(".*" + text + ".*", "i");

        tree.filterBy(function (node) {
            return re.test(node.text);
        });
    }
};

var clearFilter = function () {
    var field = TriggerField1;

    field.setValue("");
    tree.clearFilter();
    tree.getRootNode().collapseChildNodes(true);
    tree.getRootNode().ensureVisible();
};